package com.matrix.draft.sockettest.day9;

import com.matrix.draft.sockettest.day9.pojo.GlobalProto;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class MultiProtoClientHandler extends SimpleChannelInboundHandler<String> {

    // 向服务器发送一个Proto生成的Java对象
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws InterruptedException {
        // new
        GlobalProto.Global chocolate = GlobalProto.Global.newBuilder()
                .setJavaType(GlobalProto.Global.JavaType.Chocolate)
                .setChocolate(GlobalProto.Chocolate.newBuilder()
                        .setId(1)
                        .setName("霸王巧克力")
                        .setNPieces(8)
                        .build()
                )
                .build();
        GlobalProto.Global milk = GlobalProto.Global.newBuilder()
                .setJavaType(GlobalProto.Global.JavaType.Milk)
                .setMilk(GlobalProto.Milk.newBuilder()
                        .setId(2)
                        .setName("蒙牛牛奶")
                        .setSweetness(50)
                        .build()
                )
                .build();
        GlobalProto.Global banana = GlobalProto.Global.newBuilder()
                .setJavaType(GlobalProto.Global.JavaType.Banana)
                .setBanana(GlobalProto.Banana.newBuilder()
                        .setId(3)
                        .setName("海南香蕉")
                        .setColor("yellow")
                        .build()
                )
                .build();
        // 发送3次不同类型数据
        Thread.sleep(1000);
        ctx.writeAndFlush(chocolate);
        Thread.sleep(1000);
        ctx.writeAndFlush(milk);
        Thread.sleep(1000);
        ctx.writeAndFlush(banana);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) {
        System.out.println(msg);
    }
}
